﻿using AuthTemplate.DTO.Common;
using AuthTemplate.DTO.Relevance;
using AuthTemplate.Entity;
using AuthTemplate.Infrastructure;
using AuthTemplate.Infrastructure.Token;
using AuthTemplate.MVC.Filter;
using AuthTemplate.Service;
using AuthTemplate.ViewModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace AuthTemplate.MVC.Controllers
{
    [LoginFilter]
    [RoutePrefix("relevance")]
    public class RelevanceController : BaseController
    {
        private readonly IRelevanceService relevanceService;

        public RelevanceController(IRelevanceService relevanceService)
        {
            this.relevanceService = relevanceService;
        }

        [HttpGet]
        [Route("getAllRelevanceType")]
        public string GetAllRelevanceType()
        {
            MyResponse<List< DicDto >> resp = new MyResponse<List<DicDto>>();
            try
            {
                Dictionary<string, string> dic = relevanceService.GetAllRelevanceType();
                List<DicDto> dtos = DicDto.DicToDto(dic);
                resp.data = dtos;
            }
            catch (Exception ex)
            {
                resp.Code = 500;
                resp.Message = "操作失败" + ex.Message;
            }
            return JsonHelper.Serialize(resp);
        }

        [HttpPost]
        [Route("addRelevance")]
        public string AddRelevance(AddRelevanceInput input)
        {
            MyResponse resp = new MyResponse();
            try
            {
                string token = HttpContext.Request.Headers["Authorization"].Substring(7).Trim();
                string userId = CommonToken.DecodeToken(token, false).UserID;

                input.OperatorId = userId;
                input.OperateTime = DateTime.Now;
                relevanceService.AddRelevance(input);
            }
            catch (Exception ex)
            {
                resp.Code = 500;
                resp.Message = "操作失败" + ex.Message;
            }
            return JsonHelper.Serialize(resp);
        }

        [HttpPost]
        [Route("editRelevance")]
        public string EditRelevance(EditRelevanceInput input)
        {
            MyResponse resp = new MyResponse();
            try
            {
                string token = HttpContext.Request.Headers["Authorization"].Substring(7).Trim();
                string userId = CommonToken.DecodeToken(token, false).UserID;

                input.OperatorId = userId;
                input.OperateTime = DateTime.Now;
                relevanceService.EditRelevance(input);
            }
            catch (Exception ex)
            {
                resp.Code = 500;
                resp.Message = "操作失败" + ex.Message;
            }
            return JsonHelper.Serialize(resp);
        }

        [HttpGet]
        [Route("getAllRelevance")]
        public string GetAllRelevance(int page, int limit, string key ,string firstId )
        {
            TableData<List<Relevance>> resp = new TableData<List<Relevance>>();
            try
            {
                int count = 0;
                var relevances = relevanceService.GetAllRelevance(page, limit, key, firstId, out count);
                resp.data = relevances;
                resp.code = 0;
                resp.msg = "操作成功";
                resp.count = count;
            }
            catch (Exception ex)
            {
                resp.code = 500;
                resp.msg = "操作失败" + ex.Message;
                resp.count = 0;
            }
            return JsonHelper.Serialize(resp);
        }

        [HttpPost]
        [Route("deleteRelevance")]
        public string DeleteRelevance(string id)
        {
            MyResponse resp = new MyResponse();
            try
            {
                relevanceService.DeleteRelevance(id);
            }
            catch (Exception ex)
            {
                resp.Code = 500;
                resp.Message = "操作失败" + ex.Message;
            }
            return JsonHelper.Serialize(resp);
        }
    }
}